package com.my.rabbitmq.three;

import com.my.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 林允
 * @version 1.0
 * @description: TODO
 * @date 2023/11/25 15:26
 */
// 消费者
public class Worker02 {

    // 队列名称
    public static final String ACK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C2等待接收消息时间较长");

        DeliverCallback deliverCallback = (consumerTag, message) -> {

            // 沉睡30s
            try {
                Thread.sleep(30 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("consumerTag = " + consumerTag);
            System.out.println(new String(message.getBody()));
            System.out.println("C2消息接收成功");
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        channel.basicQos(5);

        boolean autoACK = false;
        channel.basicConsume(ACK_QUEUE_NAME, autoACK, deliverCallback, (consumerTag) -> {
            System.out.println("C2消息接收失败");
        });

    }

}
